Page({
  data: {
    printList: [{
      "id": 10,
      "value": "考研数学660题.docx",
      "pages": 100,
      "desc": "",
      "img": "../../images/11231.jpg",
      "imgType": "../../images/word1.png",
      "selected": true,
      "num": 1,
      "price": 10.1,
    },
    {
      "id": 21,
      "value": "张宇考研数学1000题.docx",
      "pages": 100,
      "desc": "",
      "img": "../../images/11231.jpg",
      "imgType": "../../images/word1.png",
      "selected": true,
      "num": 1,
      "price": 10.2,
    },], // 待打印列表
    hasList: false, //是否有数据
    totalPrice: 0, //价格
    selectAll: true //是否全选，默认全选
  },
  addCount(e) {
    const index = e.currentTarget.dataset.index;
    let printList = this.data.printList;
    let num = printList[index].num;
    num = num + 1;
    printList[index].num = num;
    this.setData({
      printList: printList
    });
    this.getTotalPrice();
  },
  minusCount(e) {
    const index = e.currentTarget.dataset.index;
    let printList = this.data.printList;
    let num = printList[index].num;
    if (num <= 1) {
      return false;
    }
    num = num - 1;
    printList[index].num = num;
    this.setData({
      printList: printList
    });
    this.getTotalPrice();
  },
  deleteList(e){
    const index = e.currentTarget.dataset.index;
    let printList = this.data.printList;
    printList.splice(index,1);
    this.setData({
      printList:printList
    });
    if(!printList.length){
      this.setData({
        hasList:false
      });
    }else{
      this.getTotalPrice();
      }
  },
  selectList(e) {
    const index = e.currentTarget.dataset.index;
    let printList = this.data.printList;
    const selected = printList[index].selected;
    printList[index].selected = !selected;
    this.setData({
      printList: printList
    })
    this.getTotalPrice();
  },
  selectAll(e) {
    let selectAll = this.data.selectAll;
    selectAll = !selectAll;
    let printList = this.data.printList;
    for (let i = 0; i < printList.length; i++) {
      printList[i].selected = selectAll;
    }
    this.setData({
      selectAll: selectAll,
      printList: printList
    })
    this.getTotalPrice();
  },
  getTotalPrice() {
    let printList = this.data.printList;
    let total = 0;
    for (let i = 0; i < printList.length; i++) {
      if (printList[i].selected) {
        total += printList[i].price * printList[i].num

      }
    }
    this.setData({
      printList: printList,
      totalPrice: total.toFixed(1)
    })
  },
  toEdit(e){
    var index = e.currentTarget.dataset.index
    console.log(index)
    // 要跳转的页面路径
    var targetPagePath = '../print/edit?value='+ index;
    console.log(targetPagePath)
    // 使用wx.navigateTo方法跳转到指定页面
    wx.navigateTo({
      url: targetPagePath,
      disableScroll: true
    });
  },
  look(e){

  },
  onShow() {
    this.getTotalPrice();
    
  },
  onReady: function () {
    this.popup = this.selectComponent("#popup");
  },
  changeRange() {
    this.popup.changeRange();
    this.setData({
      hasList: true,
    })
  }
})
